package molydb

import (
	"molydb/opt"
	"molydb/storage"
	"os"
)

type session struct {
	stor     *iStorage
	storLock storage.Locker
	o        *cachedOptions
	tops     *tOps

	refCh   chan *vTask
	relCh   chan *vTask
	deltaCh chan *vDelta
	abandon chan int64
	closeC  chan struct{}

	fileRefCh chan chan map[int64]int
}

func newSession(stor storage.Storage, o *opt.Options) (s *session, err error) {
	if stor == nil {
		return nil, os.ErrInvalid
	}
	storLock, err := stor.Lock()
	if err != nil {
		return
	}
	s = &session{
		stor:      newIStorage(stor),
		storLock:  storLock,
		refCh:     make(chan *vTask),
		relCh:     make(chan *vTask),
		deltaCh:   make(chan *vDelta),
		abandon:   make(chan int64),
		fileRefCh: make(chan chan map[int64]int),
		closeC:    make(chan struct{}),
	}

	s.setOptions(o)
	s.tops = newTableOps(s)
}
